<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<jsp:include page="/common/commonCss.jsp" />
<jsp:include page="/common/commonJs.jsp" />
<script type="text/javascript">
	var isEdit = false;
	$(document).ready(function (){
		initPageBean();
	});
	
	function initPageBean() {
		var form = $("#CusCpndefForm");
		var pagenbean = new PageBean(form);
		pagenbean.setTableId("CusCpndefTable");
		pagenbean.setCallback(initTable);
		pagenbean.setFootId("CusCpndefFoot");
		setPagebeanObj(pagenbean);
		pagenbean.doPage();
	}
	
	function initTable(data)
	{
		if(data == null)
			{
				return;
			}
		trimJsonArray(data);
		$.each(data,function(ind, obj){
			var i = ind + 1;
			var discountType = "";
			var amt = "";
			var multiUse = "";
			var type = obj.discountType;
			var multi = obj.multiUse;
			if(type == 1)
				{
					discountType = "折扣劵";
					amt = (obj.discountAmt*100)+"%";
				}
			else if(type == 2)
				{
					discountType = "代金劵";
					amt = obj.discountAmt+"元";
				}
			if(multi == 1)
				{
					multiUse = "单次劵";
				}
			else if(multi == 2)
				{
				multiUse = "多次劵";
				}
			var htm = "<tr>";
			htm += "<td>" + i + "</td>";
			htm += "<td class='lv'><a href='#' multiUse='"+obj.multiUse+"' discountType='"+obj.discountType+"' pkCpndefId='"+obj.pkCpndefId+"' discountAmt='"+obj.discountAmt+"' fkPrdcateId='"+obj.fkPrdcateId+"' onclick=\"addDiv(this,'true');\">" + obj.couponName + "</a></td>";
			htm += "<td>" + discountType + "</td>";
			htm += "<td>" + amt + "</td>";
			htm += "<td>" + multiUse + "</td>";
			htm += "<td>" + obj.cateName + "</td>";
			htm += "<td><input type='button' class='btn_l' value='删除' onclick=\"delYouh(this);\" pkCpndefId='"+obj.pkCpndefId+"'></td></tr>";

			$("#CusCpndefTable").find("tr:eq(" + ind + ")").after(htm);
		});
	}
	//删除优惠劵
	function delYouh(obj)
	{
		$.messager.confirm("提醒","确定要删除优惠劵吗？",function(r){
			if(r){
				var pkCpndefId = $(obj).attr("pkCpndefId");
				$.ajax({
					url:"${basePath}CusCpndef/editGblCusCpndef.shtml",
					data:{"pkCpndefId":pkCpndefId,"delFlag":"1"},
					dataType:"text",
					type:"post",
					async:false,
					success:function(result)
					{
						if(result == 1)
						{
							alertInfo($common_delete_success);
							initPageBean();
						}
						if(result == 0)
						{
							alertError($common_delete_failure);
						}
					},
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					}
				});
			}
		});
	}
	
	//新增优惠劵
	function addOrEditYouh()
	{
		if(checkCanAdd())
			{
			if(isEdit)
				{
					editYouh();
				}
			else
				{
				$.ajax({
					url:"${basePath}CusCpndef/insertGblCusCpndef.shtml",
					data:$("#addCusCpndef").serialize(),
					dataType:"text",
					type:"post",
					async:false,
					success:function(result)
					{
						if(result == 1)
						{
							alertInfo("新增优惠劵成功");
							$('#ex').window('close');
							initPageBean();
						}
						if(result == 0)
						{
							alertError("新增优惠劵失败");
						}
					},
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					}
				});
				}
			}
	}
	
	//检测有效性
	function checkCanAdd()
	{
		//优惠劵名称
		var couponNameVal = $.trim($("#couponNameVal").val());
		if(couponNameVal != null && couponNameVal != "")
			{
				$("#couponName").val(couponNameVal);
				$("#couponName2").val(couponNameVal);
			}
		else
			{
				alertError("请填写优惠劵名称");
				return false;
			}
		//折扣金额
		var discountAmtVal = $.trim($("#discountAmtVal").val());
		if(discountAmtVal != null && discountAmtVal != "")
		{
			if(!isNaN(discountAmtVal))
				{
					if($("#language_y").val() == 1)
						{
							if(parseInt(discountAmtVal) > 100)
								{
									alertError("折扣率不能大于100，请修改");
									$("#discountAmtVal").val("");
									return false;
								}
							else
								{
									$("#discountAmt2").val(parseInt(discountAmtVal)/100);
								}
						}
					$("#discountAmt").val(discountAmtVal);
				}
			else
				{
					alertError("折扣请填写数字");
					$("#discountAmtVal").val("");
					return false;
				}
			
		}
		else
		{
			alertError("请填写抵扣金额");
			return false;
		}
		//适用范围
		var fkPrdcateId = null;
		$("input[name = 'prdCateId']:checked").each(function(){
			if($(this).attr("checked"))
				{
					fkPrdcateId = $(this).val();
				}
		});
		if(fkPrdcateId != null)
			{
				$("#fkPrdcateId").val(fkPrdcateId);
				$("#fkPrdcateId2").val(fkPrdcateId);
			}
		else
			{
				alertError("请选择使用范围");
				return false;
			}
		$("#discountType").val($("#language_y").val());
		$("#multiUse").val($("#language_b").val());
		$("#discountType2").val($("#language_y").val());
		$("#multiUse2").val($("#language_b").val());
		return true;
	}
	
	//新增时重新初始化弹出窗口
	function clearDiv()
	{
		$("#couponNameVal").val("");
		$("#discountAmtVal").val("");
		$("input[name = 'prdCateId']:checked").each(function(){
			$(this).attr("checked",false);
		});
		$("#language_y option[value='2']").attr("selected",true);
		$("#language_b option[value='2']").attr("selected",true); 
		$("#amtScript").html("抵扣金额(元)：");
		refreshSelect();
	}
	
	//弹出对话框
	function addDiv(obj,edit)
	{
		if(edit == 'true')
			{
				//编辑优惠劵时在弹出窗口中自动填写数据
				isEdit = true;
				$("#couponNameVal").val($(obj).html());
				var discountType = $(obj).attr("discountType");
				if(discountType == 1)//打折劵
					{
						$("#discountAmtVal").val($(obj).attr("discountAmt")*100);
					}
				else
					{
						$("#discountAmtVal").val($(obj).attr("discountAmt"));
					}	
				
				$("input[value = '"+$(obj).attr("fkPrdcateId")+"']").attr("checked",true);
				$("#language_y option[value='"+discountType+"']").attr("selected",true);
				$("#language_b option[value='"+$(obj).attr("multiUse")+"']").attr("selected",true); 
				refreshSelect();
				$("#pkCpndefId").val($(obj).attr('pkCpndefId'));
			}
		else
			{
				isEdit = false;
				clearDiv();
			}
		$('#ex').window('open');
	}
	
	//编辑更新优惠劵
	function editYouh()
	{
		$.ajax({
				url:"${basePath}CusCpndef/editGblCusCpndef.shtml",
				data:$("#editCusCpndef").serialize(),
				dataType:"text",
				type:"post",
				async:false,
				success:function(result)
				{
					if(result == 1)
					{
						alertInfo("编辑优惠劵成功");
						$('#ex').window('close');
						initPageBean();
					}
					if(result == 0)
					{
						alertError("编辑优惠劵失败");
					}
				},
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				}
			});
	}
	
	function discountType()
	{
		if($("#language_y").val() == 1)
			{
				$("#amtScript").html("折扣率(%)：");
			}
		else if($("#language_y").val() == 2)
			{
				$("#amtScript").html("抵扣金额(元)：");
			}
	}
</script>
<title>嘉童游乐中心-优惠劵</title>
</head>
<body class="bg">
<div class="content m_auto">
	<div class="content_l mt10">
    	<ul>
    	<shiro:hasPermission name="CusCpndef/toYouh.shtml">
            <li class="content_hover"><a href="${basePath}CusCpndef/toYouh.shtml"><i class="nav_icon_p"></i><br>优惠劵</a></li>
        </shiro:hasPermission>
        </ul>
        
    </div>
    <div class="content_r mt10">
    	<!---- 内容 --->
        <div class="m20">
        <input type="button" class="btn" value="新增" onclick="addDiv(this,'false');">
        <table class="table mt10" id="CusCpndefTable">
        	<tr>
            	<th>序号</th>
                <th>优惠劵名称</th>
                <th>劵类</th>
                <th>优惠</th>
                <th>使用次数</th>
                <th>使用范围</th>
                <th>操作</th>
            </tr>
        </table>
        <!----翻页----->
        <div class="page fr m10" id="CusCpndefFoot">
        </div>
        </div>
    </div>
</div>
	<form action="${basePath}CusCpndef/findGblCusCpndef.shtml" id="CusCpndefForm" method="post">
		<input type="hidden" name="pageSize" value="10"> 
		<input type="hidden" name="pageNo" value="1"> 
	</form>
	
	<!-- 新增 -->
	<form action="" id="addCusCpndef" method="post">
		<input type="hidden" name="fkPrdcateId"id="fkPrdcateId">
		<input type="hidden" name="couponName"id="couponName">
		<input type="hidden" name="discountType"id="discountType">
		<input type="hidden" name="discountAmt"id="discountAmt">
		<input type="hidden" name="multiUse"id="multiUse">
	</form>
	<form action="" id="editCusCpndef" method="post">
		<input type="hidden" name="pkCpndefId"id="pkCpndefId">
		<input type="hidden" name="fkPrdcateId"id="fkPrdcateId2">
		<input type="hidden" name="couponName"id="couponName2">
		<input type="hidden" name="discountType"id="discountType2">
		<input type="hidden" name="discountAmt"id="discountAmt2">
		<input type="hidden" name="multiUse"id="multiUse2">
	</form>
<!---弹出层--->
<div id="ex" class="easyui-window" title="新增" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:480px; height:320px; position:relative;">
	<div class="m10 dl_dd_label_x mt20">
        <dl class="fixed">
            <dd><label>优惠券名称：</label><input type="text" class="iput_t fl w200" id="couponNameVal"><span class="color_red ml5">*</span></dd>
            <dd><label>券类：</label>
            	<div class="uboxstyle">
                    <select name="language_y" id="language_y" onchange="discountType();">
                        <option value="2" selected="selected">代金券</option>
                        <option value="1">折扣券</option>
                    </select>
                </div>
                <span class="color_red ml5">*</span>
            </dd>
            <dd><label><span id="amtScript">抵扣金额：</span></label><input type="text" class="iput_t fl w200" id="discountAmtVal"><span class="color_red ml5">*</span></dd>
            <dd><label>使用次数：</label>
            	<div class="uboxstyle">
                    <select name="language_b" id="language_b">
                        <option value="2" selected="selected">多次券</option>
                        <option value="1">单次劵</option>
                    </select>
                </div>
                <span class="color_red ml5">*</span>
            </dd>
            <dd><label>选择使用范围：</label>
            	<ul class="ul_radio fl">
                    <li><input type='radio' value='001001' name='prdCateId' class="mr5">单次票</li>
                    <li><input type='radio' value='001002' name='prdCateId' class="mr5">会籍</li>
					<li><input type='radio' value='001003' name='prdCateId' class="mr5">派对</li>
                </ul>
                <span class="color_red ml5">*</span>
            </dd>
        </dl>
    </div>
    <div class="btm_btn btm_bg">
    	<dl>
        	<dd><input type="button" class="btn" onclick="addOrEditYouh();" value="确定"></dd>
            <dd><input type="button" class="btn" onclick="$('#ex').window('close')" value="返回"></dd>
        </dl>
    </div>
</div>
<!---弹出层--->

</body>
</html>